package com.mirror.client;

import java.util.Date;

import com.arendar.alexander.gwt.forum.client.Forum;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

public class Mirror implements EntryPoint {
	
	public static LynchServiceAsync lynchService;
	public static String postDate;
	public static Image preloaderImage;
	public static FlowPanel preloader;
	
	Date date = new Date(); 
	
	HTMLPanel htmlPanel;
	FlowPanel page;
	ScrollPanel scroller;
	FlowPanel container;
	
	
	PushButton previousButton;
	PushButton nextButton;	
	
	Button newCommentButton;
	
	public void onModuleLoad() {
		preloaderImage = new Image("/images/icons/preloader.gif");
		preloader = new FlowPanel();
		preloader.add(preloaderImage);
		lynchService = (LynchServiceAsync)GWT.create(LynchService.class);
		container = new FlowPanel();
		page = new FlowPanel();
		
		Image previousImgUp = new Image("/images/buttons/previous.png");
		Image previousImgDown = previousImgUp;
		previousButton = new PushButton(previousImgUp, previousImgDown);
		DOM.setElementAttribute(previousButton.getElement(), "id", "previousButton");
		previousButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				date = new Date(date.getTime() - 1000 * 60 * 60 * 24);
				if (new Date().getTime() - date.getTime() > 1000 * 60 * 60 * 24)
				{
					nextButton.setEnabled(true);
				}
				fillBusinessLynchBlock();
			}
		});
		
		Image nextImgUp = new Image("/images/buttons/next.png");
		Image nextImgDown = nextImgUp;
		nextButton = new PushButton(nextImgUp, nextImgDown);
		DOM.setElementAttribute(nextButton.getElement(), "id", "nextButton");
		if (new Date().getTime() - date.getTime() < 1000 * 60 * 60 * 24)
		{
			nextButton.setEnabled(false);
		}
		nextButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				date = new Date(date.getTime() + 1000 * 60 * 60 * 24);
				fillBusinessLynchBlock();
				if (new Date().getTime() - date.getTime() < 1000 * 60 * 60 * 24)
				{
					nextButton.setEnabled(false);
				}
			}
		});
		
		FlowPanel navigationBlock = new FlowPanel();
		navigationBlock.add(previousButton);
		Label showUserWhereToGo = new Label ("Оставлять свои комментарии можно cнизу");
		showUserWhereToGo.setStyleName("show_user_way");
		navigationBlock.add(showUserWhereToGo);
		navigationBlock.add(nextButton);
		container.add(navigationBlock);		
		
		scroller = new ScrollPanel(page);
		RootPanel.get("navigation_holder").add(container);
		RootPanel.get("content_holder").add(scroller);
		
		fillBusinessLynchBlock();
	}
	
	private native void showLynchComments()
	/*-{
		$wnd.showCrap();
	}-*/;
	
	private void fillBusinessLynchBlock()
	{
		page.clear();
		page.add(preloader);
		lynchService.getDateFormatted(date, new AsyncCallback<String>(){
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onSuccess(String result) {
				postDate = result;
				lynchService.getBusinessLynchBlock(date, new AsyncCallback<String>(){
					public void onSuccess(String result)
					{
						preloader.removeFromParent();
						htmlPanel = new HTMLPanel(result);
						page.add(htmlPanel);
						Forum forum = new Forum("linch", postDate, "artlebedevmirror@gmail.com", "Business Lynch Comments");
						page.add(forum);
						scroller.clear();
						scroller.add(page);
						showLynchComments();
						forum.showComments();
					}
					
					public void onFailure(Throwable caught)
					{
						
					}
				});
			}
		});
	}	
}
